package com.jiazhong.mingxing.boot.boot11.过滤消息;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.MessageSelector;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

@Slf4j
public class 过滤消息消费 {
    @SneakyThrows
    public static void main(String[] args) {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("jiazhong-consumer-b");
        consumer.setNamesrvAddr("124.220.170.254:9876");
        // 设置筛选条件: 性别：女  年龄：22 城市：西安或者宝鸡
        consumer.subscribe("TopicE", MessageSelector.bySql("gender='女' and age>=22 and (city='西安' or city='宝鸡')"));
        consumer.registerMessageListener((MessageListenerConcurrently) (list, consumeConcurrentlyContext) -> {
            try {
                log.info("message:{}", new String(list.get(0).getBody()));
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                return ConsumeConcurrentlyStatus.RECONSUME_LATER;
            }
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        });
        consumer.start();
    }
}
